using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace SmartMathLibrary.Space
{
    /// <summary>
    /// This class provides methods to find out the fields, values and types of a class instance.
    /// </summary>
    [Serializable]
    public class ValueAnalyzer : AbstractAnalyzer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ValueAnalyzer"/> class.
        /// </summary>
        /// <param name="analyzerObject">The object to analyze.</param>
        public ValueAnalyzer(Object analyzerObject)
            : base(analyzerObject)
        {
        }

        /// <summary>
        /// Analyzes the fields, values and types of the specified class instance.
        /// </summary>
        /// <returns>The fields, values and types of the specified class instance.</returns>
        public Field[] AnalyzeValues()
        {
            List<Field> result = new List<Field>();
            FieldAnalyzer analyzer = new FieldAnalyzer(this.AnalyzerObject);

            foreach (FieldInfo field in analyzer.AnalyzeFields())
            {
                bool add = false;
                object[] attributes = field.GetCustomAttributes(true);

                for (int i = 0; i < attributes.Length; i++)
                {
                    if (attributes[i] is SpaceAttribute)
                    {
                        add = true;
                        break;
                    }
                }

                if (add)
                {
                    result.Add(new Field(field.Name, field.GetValue(this.AnalyzerObject),
                                         field.FieldType));
                }
            }

            return result.ToArray();
        }
    }
}